module.exports=class
{
    constructor(step,grade)
    {
        this.grade = grade;
        this.step = step;
        this.stepType = '';
        this.awardCount = 0;
        this.awardItems = [];
        this.gradeData=[];
        this.sortedGradeData=[];
        this.awardItemsCount= this._getAwartItemsCount();
        this.judgerCount =this._getJudgerCount();
    }


    //获取处理好的数据
    list()
    {
        //获得比赛结果
        this._getGradeList();
        //处理获奖还是淘汰
        return this._getCPResult();

    }

    _getCPResult(){
        //处理晋级还是颁奖
        this._getAwardCondition();
        this._sortGradeData();
        let gradeList=[];
        let data=this.sortedGradeData;
        if(this.stepType=='晋级'){
            //排序,并获得前五
            for (let i in data) {
                let nowGrade=data[i];
                if(i<this.awardCount){
                    nowGrade.Res='晋级';
                }else{
                    nowGrade.Res='淘汰';
                }
                gradeList.push(nowGrade);
            }
            return gradeList;

        }else if(this.stepType=='颁奖'){
            //获得几等奖处理
            let nowIndex=0;
            for(let i=0;i<this.awardItemsCount&&nowIndex<this.sortedGradeData.length;i++){
                for(let j=0;j<this.awardItems[i].Num&&nowIndex<this.sortedGradeData.length;j++){
                    let nowGrade=data[nowIndex];
                    nowGrade.Res=this.awardItems[i].Title;
                    nowIndex++;
                    gradeList.push(nowGrade);
                }
            }
            for(nowIndex;nowIndex<this.sortedGradeData.length&&nowIndex<this.sortedGradeData.length;nowIndex++){
                let nowGrade=data[nowIndex];
                nowGrade.Res='淘汰';
                gradeList.push(nowGrade)
            }

            return gradeList;

        }

    }

    _getAwartItemsCount(){
        return this.step.stepsaward.length;
    }
    _sortGradeData(){
        let res=[];
        let grades=this.gradeData;
        for (let index=0;index<grades.length;index++) {
            let now=grades[index];
            let nowGrade=now.TotalGrade;
            for(let j=index+1;j<grades.length;j++){
                if(nowGrade<grades[j].TotalGrade){
                    let t=now;
                    now=grades[j];
                    grades[j]=t;
                }
            }
            res.push(now);

        }
        this.sortedGradeData=res;
    }


    _getJudgerCount()
    {
       return this.step.competitionstepstojudger.length;
    }

    _getAwardCondition()
    {
        let steps = this.step;
        if (steps.Type == '晋级')
        {
            //获取颁奖人数
            this.stepType = steps.Type;
            this.awardCount = steps.QualifiedPerson;
        } else if (steps.Type == '颁奖')
        {
            this.stepType = steps.Type;
            this.awardItems = [];
            this._getStepsAwardItems();
            //处理晋级
        } else
        {
            return;
        }

    }

    _getStepsAwardItems()
    {
        let totalCount = 0;
        let items = this.step.stepsaward;
        for (let index in items)
        {
            let award = items[index];
            let awardObj = {}
            awardObj.Title = award.Title;
            awardObj.Num = award.Num;
            totalCount += award.Num;
            this.awardItems.push(awardObj);
        }
        this.awardCount = totalCount;
    }

    _getGradeList()
    {
        let grade = this.grade;
        let gradeArr = [];
        for (let index in grade)
        {
            let item = grade[index];
            let oneGrade = {};
            oneGrade.Title = item.works.Title;
            oneGrade.Auther = item.works.Auther;
            oneGrade.TotalGrade = 0;
            oneGrade.Grade = [];


            for (let index=0;index<this.judgerCount;index++)
            {
                let gradeItem = item.grade[index];
                let grade;
                if(gradeItem){
                    grade = gradeItem.Grade;

                }
                grade = grade > 0 ? grade : 0;
                oneGrade.Grade.push(grade);
                oneGrade.TotalGrade += grade;

            }
            gradeArr.push(oneGrade);
        }
        this.gradeData = gradeArr;

    }
};



